/*
 *  Copyright 2011 Alexey Andreev.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.xthl.core.data;

import org.nop.migration.AbstractMigration;
import org.nop.migration.ChangeSetBuilder;

/**
 *
 * @author Alexey Andreev
 */
public class Migration0 extends AbstractMigration {
    @Override
    protected void apply(ChangeSetBuilder cs) {
        cs.createTable("Users")
                .with().column("id").integer().primaryKey()
                .with().column("login").varchar(40).unique()
                .with().column("passwordHash").varchar(100).nullable()
                .with().column("creationDate").timestamp()
                .with().column("enabled").bool().defaultValue(true)
                .with().column("activated").bool().defaultValue(false)
                .with().column("email").varchar(250).nullable().defaultValue(null)
                .with().column("activationHash").varchar(100).nullable().defaultValue(null);
        cs.createTable("Roles")
                .with().column("id").integer().primaryKey()
                .with().column("name").varchar(80)
                .with().column("system").bool().defaultValue(false);
        cs.createTable("Permissions")
                .with().column("id").integer().id()
                .with().column("name").varchar(40).unique();
        cs.createTable("UserRoles")
                .with().column("userId").integer().reference().to("Users", "id")
                .with().column("roleId").integer().reference().to("Roles", "id")
                .with().primaryKey().on("userId", "roleId");
        cs.createTable("UserPermissions")
                .with().column("userId").integer().reference().to("Users", "id")
                .with().column("permissionId").integer().reference().to("Permissions", "id")
                .with().primaryKey().on("userId", "permissionId");
        cs.createTable("RolePermissions")
                .with().column("roleId").integer().reference().to("Roles", "id")
                .with().column("permissionId").integer().reference().to("Permissions", "id")
                .with().primaryKey().on("roleId", "permissionId");
        cs.createTable("DefaultRoles")
                .with().column("type").varchar(20).check().values("guest", "default")
                .with().column("roleId").integer().reference().to("Roles", "id")
                .with().primaryKey().on("type", "roleId");
        cs.createTable("MenuItems")
                .with().column("id").integer().primaryKey()
                .with().column("itemType").varchar(7).check()
                    .values("special", "custom")
                .with().column("pos").integer()
                .with().column("enabled").bool()
                .with().column("title").varchar(100)
                .with().column("target").varchar(250);
        cs.createSequence("MenuItemsSequence", 1);
    }
}
